package LeetCode.leetcode54;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Solution {
    public static void main(String[] args) {
        int[][] test = new int[][]{
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12},
//                {13,14,15,16}
        };
        new Solution().spiralOrder(test);
    }
    public List<Integer> spiralOrder(int[][] matrix) {
        int m = matrix.length, realRow = matrix.length, realCol = matrix[0].length, n = matrix[0].length;
        int loop = m / 2;
        List<Integer> res = new ArrayList<>();
        int curLoop = 0;
        int i = 0;
        int j = 0;
        while (curLoop < loop) {

            for (; j < n - 1; j++) {
                res.add(matrix[i][j]);
            }


            for(; i < m - 1; i++){
                res.add(matrix[i][j]);
            }

            for(; j > curLoop; j--){
                res.add(matrix[i][j]);
            }

            for(; i > curLoop; i--){
                res.add(matrix[i][j]);
            }
            i++;
            j++;
            m--;
            n--;
            curLoop++;
        }
        if (realRow % 2 == 1){
            for (; j < realCol - curLoop; j++) {
                res.add(matrix[i][j]);
            }
        }
        System.out.println(res);
        return res;
    }
}